#!/usr/bin/env bash
# Automatically update versions in files and commit changes

set -e

NEW_VERSION=$1

if [ -z "$NEW_VERSION" ]; then
    NEW_VERSION=$(jq -r ".version" manifest.json | awk -F. -v OFS=. '{$NF += 1 ; print}')
fi

echo "Releasing version '${NEW_VERSION}'"

# Delete old files if they exist
rm -f package.tmp.json
rm -f manifest.tmp.json

# Rewrite versions in relevant files.
jq ".version=\"${NEW_VERSION}\"" package.json > package.tmp.json && mv package.tmp.json package.json
jq ".version=\"${NEW_VERSION}\"" manifest.json > manifest.tmp.json && mv manifest.tmp.json manifest.json
jq ".version=\"${NEW_VERSION}\"" manifest-beta.json > manifest-beta.tmp.json && mv manifest-beta.tmp.json manifest-beta.json
jq ". + {\"${NEW_VERSION}\": \"1.9.0\"}" versions.json > versions.tmp.json && mv versions.tmp.json versions.json

npm run build && npm run docs

# Create commit & commit.
git commit -a -m "Auto-release ${NEW_VERSION}"
git push

